package com.cmcc.xdr.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class JDBCUtil {
	private static Logger logger = LogManager.getLogger(JDBCUtil.class);
	private static String driverClassName;
	private static String url;
	private static String user;
	private static String password;
	static {
		try {
			InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("config/cmcc-sdtp-dev.properties");
			Properties props = new Properties();
			props.load(in);
			driverClassName = props.getProperty("datasource.driverClassName");
			url = props.getProperty("datasource.url");
			user = props.getProperty("datasource.userName");
			password = props.getProperty("datasource.efarmpd");
			Class.forName(driverClassName);
		} catch (Exception e) {
			logger.error("JDBC连接失败:",e);
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws Exception {
		return DriverManager.getConnection(url, user, password);

	}

	public static void release(ResultSet rs, Statement stmt, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception e) {
				logger.error("关闭连接失败:",e);
				e.printStackTrace();
			}
			rs = null;
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception e) {
				logger.error("关闭连接失败:",e);
				e.printStackTrace();
			}
			stmt = null;
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
				logger.error("关闭连接失败:",e);
				e.printStackTrace();
			}
			conn = null;
		}
	}
}